package LeetCode;

import util.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution111 {

    public static void main(String[] args) {
        new Solution111().minDepth(null);
    }

    public int minDepth(TreeNode root) {
        // 如果是一棵空树，最小深度为0，如果只有根节点，最小深度为1
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        // 左右两个子树深度的最小值
        return Math.min(dip(root.left), dip(root.right)) +1;
    }

    private int dip(TreeNode root){
        if(root == null){
            return 0;
        }
        return Math.min(dip(root.left),dip(root.right))+1;
    }
}